package com.zygk.library.util;


  

import java.io.ByteArrayOutputStream;
import java.security.MessageDigest;

/**
 * Created by xiang.li on 2015/2/11.
 */
public class SHA {
    /**
     * 定义加密方式
     */
    private final static String KEY_SHA = "SHA";
    private final static String KEY_SHA1 = "SHA-1";
    /**
     * 全局数组
     */
    private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    /**
     * 构造函数
     */
    public SHA() {

    }

    /**
     * SHA 加密
     * @param data 需要加密的字节数组
     * @return 加密之后的字节数组
     * @throws Exception
     */
    public static byte[] encryptSHA(byte[] data) throws Exception {
        // 创建具有指定算法名称的信息摘要
//	        MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
        MessageDigest sha = MessageDigest.getInstance(KEY_SHA1);
        // 使用指定的字节数组对摘要进行最后更新
        sha.update(data);
        // 完成摘要计算并返回
        return sha.digest();
    }

    /**
     * SHA 加密
     * @param data 需要加密的字符串
     * @return 加密之后的字符串
     * @throws Exception
     */
    public static String encryptSHA(String data) throws Exception {
        // 验证传入的字符串
        if (StringUtils.isBlank(data)) {
            return "";
        }
        // 创建具有指定算法名称的信息摘要
        MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
        // 使用指定的字节数组对摘要进行最后更新
        sha.update(data.getBytes());
        // 完成摘要计算
        byte[] bytes = sha.digest();
        // 将得到的字节数组变成字符串返回
        return byteArrayToHexString(bytes);
    }

    /**
     * 将一个字节转化成十六进制形式的字符串
     * @param b 字节数组
     * @return 字符串
     */
    private static String byteToHexString(byte b) {
        int ret = b;
        //System.out.println("ret = " + ret);
        if (ret < 0) {
            ret += 256;
        }
        int m = ret / 16;
        int n = ret % 16;
        return hexDigits[m] + hexDigits[n];
    }

    /**
     * 转换字节数组为十六进制字符串
     * @param bytes 字节数组
     * @return 十六进制字符串
     */
    private static String byteArrayToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(byteToHexString(bytes[i]));
        }
        return sb.toString();
    }

    /**
     * 字符串转换为16进制字符串
     *
     * @param s
     * @return
     */
    public static String stringToHexString(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }

  //java 合并两个byte数组
    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){
        byte[] byte_3 = new byte[byte_1.length+byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }

    /**
     * 测试方法
     * @param args
     */
    public static void main(String[] args) throws Exception {
        String key = "*1234567";
        byte b[] = key.getBytes();
//	        System.out.println(new String(encryptSHA(b)));


//	        byte b1[] = BytesHexStrTranslate.toBytes("02b5c969be625cb710705674fc67e5e614d88756");
//	        byte b_n[] = encryptSHA(b1);
//	        System.out.println(BytesHexStrTranslate.bytesToHexFun1(b_n));
        String s = "appid=pklt2017&nonce=abc123456&pageNo=1&pageSize=20&timestamp=1497599180";
        byte b1[] = BytesHexStrTranslate.toBytes("cc5327330c5fe07848c496da77cb4962d04b3cc6");
        byte b_n[] = encryptSHA(byteMerger(s.getBytes(),b1));
        System.out.println(BytesHexStrTranslate.bytesToHexFun1(b_n));

//	        byte[] c = new byte[b1.length];
//	        for(int i = 0; i < 8; i++){
//	            c[i] = a[i] ^ b_n[i];
//	        }
//	        String s1 = "02b5c969be625cb710705674fc67e5e614d88756";
//	        String s2 = "cc5327330c5fe07848c496da77cb4962d04b3cc6";
////	        String s3= "7f3b363427de5482dbb8a6fef3b24feb83bb647d";
//	        String s3 = encryptSHA(s+s2);
//	        System.out.println(s3);
        ;
    }
}
